/**
 * 这个作为总控
 * @type {[type]}
 */

var ajax = require('ajax');
const common = require('common');

cc.Class({
    extends: cc.Component,

    properties: {
        //这两个list，选，没有script
        localbooklist: {
            default: null,
            type: cc.Node
        },
        remotebooklist: {
            default: null,
            type: cc.Node
        },

        bookshelfcontent: cc.Node,

        bookstorecontent: {
            default: null,
            type: cc.Node
        }

    },

    // use this for initialization
    onLoad: function() {
        //初始化script
        this.bookshelfcontent = this.bookshelfcontent.getComponent('bookshelfcontent');

        //2.当进入书城时，判断是否能下载成功书城的JSON
        //3.书城
        //     ajax.getJSON('http://odxmd36pu.bkt.clouddn.com/bookstore.json',  {a:'11'}, function(data,zz,zzs){
        //           cc.log('bookstorejson----------->'+data);
        //       }
        //   );
        let self = this;
        cc.log('now:' + new Date().getTime());
        // cc.log(cc.js);
        // cc.loader.load('http://odxmd36pu.bkt.clouddn.com/d1A1/03.mp3',function(err,res){
        //     cc.log('---->'+res);
        //     cc.audioEngine.playMusic(res);
        // });

        //cc.game.addPersistRootNode(this.common);
        cc.loader.load(jsb.fileUtils.getWritablePath() + "bookdata.json", function(err, booklocal) {
               
             if (booklocal===null) {
            //      cc.log('common.init'+common.init);
                booklocal = []; 
             }
             cc.log('booklocal'+booklocal+booklocal.length);
            if (booklocal.length === 0) {
                //self.common.getChildByName('toast').getComponent('toast').show('您有一本绘本没有下载，请到书城下载', function() {
                common.init.toast('您有一本绘本没有下载，请到书城下载', function() {
                    cc.log('create local bookdata.json');
                    //本地还没有，要建立一个空的
                    jsb.fileUtils.writeStringToFile('[]', 'bookdata.json');
                });
            }
            common.init.bookdata = booklocal;
            // cc.log('-----bookshelf onload---'+self.bookshelfcontent);
            //1.加载上次保存的书架 最后阅读的书，级别(按时间排序)
            self.bookshelfcontent.init();
            //2.当进入书城时，判断是否能下载成功书城的JSON
            //3.书城---->不在这里进行刷新，而是在点按钮后再刷新
            // self.bookstorecontent.getComponent('bookstorecontent').init();
        });

        //再按一次退出
        let i = 0;
        cc.eventManager.addListener({
            event: cc.EventListener.KEYBOARD,
            onKeyReleased: function(keyCode, event) {
                cc.log(i);
                if (keyCode == cc.KEY.back || keyCode == cc.KEY.backspace) {
                    i++;
                    if (i == 2) {
                        cc.director.popScene();
                    } else {
                        common.init.toast('再按一次退出', function() {
                            cc.log('第一次按退出');
                        });
                    }
                }
            }
        }, this);
        this.schedule(function() {
            i = 0;
        }, 2);

    },
    /**
     * 按钮事件统一处理
     * @return {[type]} [description]
     */
    bookshelf: function() {
        //this.localbooklist.active = true;
        this.remotebooklist.active = false;
    },
    bookstore: function() {
        this.bookstorecontent.getComponent('bookstorecontent').init();
        this.remotebooklist.x = 0;
        this.remotebooklist.y = this.localbooklist.y;
        //this.localbooklist.active = false;
        this.remotebooklist.active = true;
    },
    level1: function() {
        common.init.toast('level1', function() {
            cc.log('level1');
        });
    }

});